"""
视频常见有三种类型：
1、直球类型 mp4
2、blog:http 音频和视频分离  用ffmpeg将视频和音频合并
3、m3u8 ts视频切片

解除js混淆：
工具 https://lelinhtinh.github.io/de4js/
常见的混淆：
AAendode  将js转换成颜文字表情  靶场：https://spa11.scrape.center/
JJencode  将js转换成类似于颜文字的数据 靶场：https://spa10.scrape.center/  将最后一个括号去掉就会返回一个js函数
jsfuck    [] () + !组成  靶场：https://spa12.scrape.center/

"""

"""
b站视频下载
请求资源地址
https://upos-sz-mirror08c.bilivideo.com/upgcxcode/05/74/25807357405/25807357405-1-100002.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1737301707&gen=playurlv2&os=08cbv&oi=2028107674&trid=c7cf7629ab0a456794c4fc00f2f17cf1d&mid=497303544&platform=pc&og=hw&upsig=739d6d4b94cf8c60bb2bb638223c4688&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,mid,platform,og&bvc=vod&nettype=0&orderid=0,3&f=d_0_0&agrr=0&bw=65327&logo=80000000

请求地址藏在
https://www.bilibili.com/bangumi/play/ss25992?from_spmid=666.23.0.0
Request Method:
GET
"""
import json
import os
import re

from xunter_utils.xunter_requests import ChromeClient


class BilibiliSpider(ChromeClient):

    def get_urls(self, url: str):
        res = self.session.get(url)
        data = re.findall(r'const playurlSSRData = (.*)\n', res.text)[0]
        dict_data = json.loads(data)
        # print(dict_data)
        long_title = dict_data['result']['play_view_business_info']['episode_info']['long_title']
        video_url = dict_data['result']['video_info']['dash']['video'][0]['base_url']
        audio_url = dict_data['result']['video_info']['dash']['audio'][0]['base_url']
        self.session.headers['Referer'] = url
        print(long_title, video_url, audio_url)
        return long_title, video_url, audio_url

    def download_video(self, name: str, src: str):
        video_res = self.send_request(src)
        with open(f'{name}.mp4', 'wb+') as fp:
            fp.write(video_res.content)
        print(f'{name}.mp4下载完成')

    def download_audio(self, name: str, src: str):
        audio_res = self.send_request(src)
        with open(f'{name}.mp3', 'wb+') as fp:
            fp.write(audio_res.content)
        print(f'{name}.mp3下载完成')

    def out_mp4(self, video_path: str, audio_path: str,out_path: str):
        """
        合并视频和音频文件
        :param video_path:
        :param audio_path:
        :return:
        执行cmd系统命令 在这之前要下载号ffmpeg 并且配置好系统环境变量
        """
        os.system(
            fr'ffmpeg -i {video_path} -i {audio_path} -c:v copy -c:a aac -strict experimental -map 0:v -map 1:a {out_path}'
        )   #格式话并且取消转移就用fr  把video_path和audio_path文件传入合并输出保存到out_path文件中
        print('合并完成')



if __name__ == '__main__':
    client = BilibiliSpider()
    # long_title, video_url, audio_url = client.get_urls(
    #     'https://www.bilibili.com/bangumi/play/ss3008?from_spmid=666.23.0.0')
    # client.download_video(long_title, video_url)
    # client.download_audio(long_title, audio_url)
    client.out_mp4('星际牛仔_天国之扉.mp4','星际牛仔_天国之扉.mp3','out星际牛仔_天国之扉.mp4')